from flask import Blueprint, request, render_template, flash, redirect, url_for, abort

from forms import ProductEditForm
from models import Product, db

mall = Blueprint('mall', __name__, template_folder='templates', static_folder='static')

@mall.route('/product/list/<int:page>')
# @login_required
def product_list(page):
    """ 商品列表 """
    page_size = 5
    name = request.args.get('name')
    if name:
        page_data = Product.query.filter(Product.name.contains(name), Product.is_valid==True) \
            .paginate(page=page, per_page=page_size)
    else:
        page_data = Product.query.filter(Product.is_valid==True).paginate(page=page, per_page=page_size)
    return render_template('index.html', page_data=page_data)

@mall.route('/product/detail/<uid>')
def product_detail(uid):
    """ 商品详情 """
    prod_obj = Product.query.filter_by(uid=uid).first_or_404()
    return render_template('product_detail.html', prod_obj=prod_obj)

@mall.route('/product/add', methods=['GET', 'POST'])
def product_add():
    """ 商品添加 """
    form = ProductEditForm()
    if form.validate_on_submit():
        # 保存到数据库
        print(form.data)
        prod_obj = Product(
            name = form.data['name'],
            content = form.data['content'],
            desc = form.data['desc'],
            types = form.data['types'],
            price = form.data['price'],
            origin_price = form.data['origin_price'],
            img = '/img/aaa.jpg',
            channel = form.data['channel'],
            buy_link = form.data['buy_link'],
            status = form.data['status'],
            sku_count = form.data['sku_count'],
            remain_count = form.data['remain_count'],
            view_count = form.data['view_count'],
            score = form.data['score'],
            is_valid = form.data['is_valid'],
            reoder = form.data['reoder']
        )
        db.session.add(prod_obj)
        db.session.commit()
        # 消息提示
        flash('新增商品成功', 'success')
        return redirect(url_for('mall.product_list', page=1))
    else:
        # 消息提示
        flash('请修改页面中的错误，然后提交', 'warning')
        print(form.errors)
    return render_template('product_add.html', form=form)

@mall.route('/product/edit/<uid>', methods=['GET', 'POST'])
def product_edit(uid):
    """ 商品编辑 """
    prod_obj = Product.query.filter_by(uid=uid, is_valid=True).first()
    if prod_obj is None:
        abort(404)
    form = ProductEditForm(obj=prod_obj)
    if form.validate_on_submit():
        prod_obj.name = form.data['name']
        prod_obj.content = form.data['content']
        db.session.add(prod_obj)
        db.session.commit()
        flash('修改成功', 'success')
        return redirect(url_for('mall.product_list', page=1))
    return render_template('product_edit.html', form=form, prod_obj=prod_obj)

@mall.route('/product/delete/<uid>', methods=['POST'])
def product_delete(uid):
    """ 商品删除 """
    prod_obj = Product.query.filter_by(uid=uid, is_valid=True).first()
    if prod_obj is None:
        return 'no'
    prod_obj.is_valid = False
    db.session.add(prod_obj)
    db.session.commit()
    return 'ok'